﻿/*-------------------------------------------------------------------------
* 命名空间: XrayDirection.ViewModels/InputNextRecipeViewModel
* 类       名: InputNextRecipeViewModel
* 功      能:  TD
* 时      间:  2024/2/23 17:10:36
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using Prism.Commands;
using Prism.Mvvm;
using XrayDirection.Common;
using XrayDirection.DAL.DataEntity;
using XrayDirection.DAL.LocalDataAccess;
using XrayDirection.Services;
using XrayDirection.Views;

namespace XrayDirection.ViewModels
{
    public class InputNextRecipeViewModel : BindableBase
    {
        private IWindowService _windowService;

        public InputNextRecipeViewModel(IWindowService windowService)
        {
            SearchRecipeCommand = new DelegateCommand(DoSearchRecipeCommand);
            SaveInputRecipeCommand = new DelegateCommand(DoSaveInputRecipeCommand);
            CancelInputRecipeCommand = new DelegateCommand(DoCancelInputRecipeCommand);
            _windowService = windowService;
        }

        /// <summary>
        /// 取消保存工单
        /// </summary>
        private void DoCancelInputRecipeCommand()
        {
            // viewModel初始化
            ResetViewModelVal();
            _windowService.CloseWindow(nameof(InputNextRecipeView));
        }

        private void ResetViewModelVal()
        {
            RecipeName = string.Empty;
            LotId = string.Empty;
            LongLengthX = 0;
            ShortLengthY = 0;
            FrontEdgeDisD = 0;
            DirectHoleDisF = 0;
            MoveOffset = 25;
            MoveOffset02 = 35;
        }

        /// <summary>
        /// 保存输入工单,主要做几件事:
        /// 1) 检查工单数据是否正确,如果有0或者是空就报保存错误.
        /// 2) 保存到数据库中去,输入工单的数据库,就是插入一条数据即可
        /// </summary>
        private void DoSaveInputRecipeCommand()
        {
            string inputError = string.Empty;
            if (RecipeName == string.Empty)
            {
                inputError += "工单号不能为空. ";
            }

            if (LotId == string.Empty)
            {
                inputError += "批号不能为空.";
            }
            if (LongLengthX == 0)
            {
                inputError += "长边靶距X不能为空. ";
            }
            if (ShortLengthY == 0)
            {
                inputError += "短边靶距Y不能为空. ";
            }
            if (FrontEdgeDisD == 0)
            {
                inputError += "前缘距D不能为空.";
            }
            if (DirectHoleDisF == 0)
            {
                inputError += "方向孔距不能为空. ";
            }
            if (MoveOffset == 0)
            {
                inputError += "第一块补偿值不能为空. ";
            }
            if (MoveOffset02 == 0)
            {
                inputError += "第二块补偿值不能为空.";
            }

            if (inputError != string.Empty)
            {
                MessageProvider.ShowError(inputError, "工单录入错误提示");
                return;
            }

            // 工单数据录入成功,保存工单到数据库,保存完毕之后,关闭窗口
            OrderEntity inputOrder = new OrderEntity()
            {
                RecipeName = RecipeName,
                LotId = LotId,
                LongLengthX = LongLengthX,
                ShortLengthY = ShortLengthY,
                FrontEdgeDisD = FrontEdgeDisD,
                DirectHoleDisF = DirectHoleDisF,
                MoveOffset = MoveOffset,
                MoveOffset02 = MoveOffset02,
                OkCount = 0,
                FailCount = 0,
            };
            DataResult saveResult = OrderAccess.AddOrder(inputOrder);
            if (saveResult.State)
            {
                MessageProvider.ShowInfo("工单保存成功", "操作成功提示");
                ResetViewModelVal();
                _windowService.CloseWindow(nameof(InputNextRecipeView));
            }
            else
            {
                MessageProvider.ShowError(saveResult.Message!, "操作失败提示");
                _windowService.CloseWindow(nameof(InputNextRecipeView));
            }
        }

        public DelegateCommand SaveInputRecipeCommand { get; private set; }
        public DelegateCommand CancelInputRecipeCommand { get; private set; }


        public DelegateCommand SearchRecipeCommand { get; private set; }
        private void DoSearchRecipeCommand()
        {

            DataResult<OrderEntity> result = OrderAccess.GetOrderByRecipeName(RecipeName!);
            if (result.State)
            {
                if (result.Data != null)
                {
                    OrderEntity orderRes = result.Data;
                    LongLengthX = orderRes.LongLengthX;
                    ShortLengthY = orderRes.ShortLengthY;
                    FrontEdgeDisD = orderRes.FrontEdgeDisD;
                    DirectHoleDisF = orderRes.DirectHoleDisF;
                    MoveOffset = orderRes.MoveOffset;
                    MoveOffset02 = orderRes.MoveOffset02;
                    LotId = orderRes.LotId;
                }
                else
                {
                    MessageProvider.ShowInfo("查询工单不存在,请重新录入工单信息", "工单不存在提示");
                }
            }
            else
            {
                MessageProvider.ShowError(result.Message!, "数据库操作失败");
            }
        }




        private string? recipeName;

        public string? RecipeName
        {
            get { return recipeName; }
            set
            {
                recipeName = value;
                RaisePropertyChanged();
            }
        }

        private string? lotId;

        public string? LotId
        {
            get { return lotId; }
            set
            {
                lotId = value;
                RaisePropertyChanged();
            }
        }


        private double longLengthX;

        public double LongLengthX
        {
            get { return longLengthX; }
            set
            {
                longLengthX = value;
                RaisePropertyChanged();
            }
        }


        private double shortLengthY;

        public double ShortLengthY
        {
            get { return shortLengthY; }
            set
            {
                shortLengthY = value;
                RaisePropertyChanged();
            }
        }

        private double frontEdgeDisD;

        public double FrontEdgeDisD
        {
            get { return frontEdgeDisD; }
            set
            {
                frontEdgeDisD = value;
                RaisePropertyChanged();
            }
        }


        /// <summary>
        /// 方向孔距离
        /// </summary>
        private double directHoleDisF;

        public double DirectHoleDisF
        {
            get { return directHoleDisF; }
            set
            {
                directHoleDisF = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 胶边距离,补偿距离(靶标到边缘距离补偿值)
        /// </summary>
        private double moveOffset;

        public double MoveOffset
        {
            get { return moveOffset; }
            set
            {
                moveOffset = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 胶边距离,补偿距离(靶标到边缘距离补偿值)第二块
        /// </summary>
        private double moveOffset02;

        public double MoveOffset02
        {
            get { return moveOffset02; }
            set
            {
                moveOffset02 = value;
                RaisePropertyChanged();
            }
        }

    }
}
